package com.im.controller;


import com.alibaba.fastjson.JSONObject;
import com.im.annotation.Jwt;
import com.im.config.JwtToken;
import com.im.dao.UserDao;
import com.im.pojo.ImUser;
import com.im.pojoClass.Result;
import com.im.pojoClass.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.HashMap;

@Api(tags = {"用户"}, value = "UserController")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private JwtToken jwtToken;

    @Value("${jwt.key}")
    private String JWT_KEY;

    /******************************************************************************************************/
@ApiOperation(value = "登陆", notes = "返回token")
@ApiImplicitParams({
        @ApiImplicitParam(name = "user_id", value = "用户id"),
        @ApiImplicitParam(name = "user_pass", value = "密码")
})
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Result login(@RequestParam(value = "user_id") String user_id,
                    @RequestParam(value = "user_pass") String user_pass) {

    // 判断用户名密码是否匹配
    ImUser login = userDao.login(user_id, user_pass);

    // 登陆失败
    if (login == null) {
        // {code:500,msg:"操作失败"}
        return new Result(ResultCode.FAILED, "用户名密码错误");
    }

    String token = null;
    try {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("user_id", login.getUser_id());
        hashMap.put("user_name", login.getUser_name());
        hashMap.put("user_img", login.getUser_img());
        token = jwtToken.getToken(hashMap);
    } catch (Exception e) {
        // {code:500,msg:"操作失败"}
        return new Result(ResultCode.FAILED, "Token生成错误");
    }

    // {code:200,msg:"成功"}
    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("token", token);

    return new Result(ResultCode.SUCCESS, hashMap);
}

    /******************************************************************************************************/
    @ApiOperation(value = "注册", notes = "返回token")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(@RequestBody ImUser user) {

        // 判断用户id是否存在
        ImUser nameExist = userDao.selectUser(user.getUser_id());
        if (nameExist != null) {
            // {code:500,msg:"ID已存在"}
            return new Result(ResultCode.FAILED, "ID已存在");
        }

        // 注册
        int register = userDao.register(user);

        // 判断注册成功
        if (register > 0) {
            // JWT
            // 设置过期时间
//            Calendar instance = Calendar.getInstance();
//            instance.add(Calendar.HOUR, 24 * 7);
//            instance.add(Calendar.SECOND, 20);
            String token = null;
            try {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("user_id", user.getUser_id());
                hashMap.put("user_name", user.getUser_name());
                hashMap.put("user_img", user.getUser_img());
                token = jwtToken.getToken(hashMap);
            } catch (Exception e) {
                // {code:500,msg:"操作失败"}
                return new Result(ResultCode.FAILED, "Token生成错误");
            }

            // {code:200,msg:"操作成功"}
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("token", token);

            return new Result(ResultCode.SUCCESS, hashMap);
        }

        // {code:500,msg:"注册失败"}
        return new Result(ResultCode.FAILED, "注册失败");
    }

    @Jwt
    @ApiOperation(value = "查找用户", notes = "返回用户信息")
    @RequestMapping(value = "/selectUser", method = RequestMethod.POST)
    public Result selectUser(@RequestParam String user_id) {
        // 根据id查找用户
        ImUser selectUser = userDao.selectUser(user_id);
        if (selectUser == null) {
            // {code:500,msg:"用户未找到"}
            return new Result(ResultCode.FAILED, "未找到用户");
        }

        // 查找成功
        return new Result(ResultCode.SUCCESS, selectUser);
    }

}